import axios from 'axios'
import { Message} from 'element-ui'
import store from '../store'

// 创建axios实例
const service = axios.create({
  baseURL: process.env.BASE_API, // api 的 base_url
  timeout: 5000 // 请求超时时间
})

service.interceptors.request.use(config => {
  return config
}, error => {
  Message({
    message: '请求异常,请重新发起请求!',
    type: 'error',
    duration: 5 * 1000
  })
  console.log(error) // for debug
  Promise.reject(error)
})

service.interceptors.response.use(
  response => {
    const res = response.data;
    console.log('axios', res)
    if (res.status !== 200) {
      Message({
        message: res.resultMsg,
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject('error')
    } else {
      return response.data
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: '响应失败',
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

// 请求方式的配置
export default service;

